package com.chenbei.controller.admin.system;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chenbei.common.annotation.OperationLog;
import com.chenbei.common.util.ApiResponseUtil;
import com.chenbei.common.util.PaginationUtil;
import com.chenbei.common.domain.vo.ApiResponse;
import com.chenbei.common.domain.vo.Pagination;
import com.chenbei.system.domain.SysPost;
import com.chenbei.system.service.ISysPostService;
import com.chenbei.system.vo.req.SysPostReq;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "岗位管理")
@RestController
@RequestMapping("/admin/system/post")
public class PostController {

    @Autowired
    private ISysPostService postService;

    @SaCheckPermission(value = "system:post:add", orRole = "root")
    @Operation(summary = "新增岗位")
    @OperationLog(moduleName = "岗位管理", eventName = "新增岗位")
    @PostMapping("add")
    public ApiResponse<Object> add(@Valid @RequestBody SysPost sysPost) {
        postService.add(sysPost);
        return ApiResponseUtil.success();
    }

    @SaCheckPermission(value = "system:post:delete", orRole = "root")
    @Operation(summary = "删除岗位")
    @OperationLog(moduleName = "岗位管理", eventName = "删除岗位")
    @PostMapping("deleteById/{id}")
    public ApiResponse<Object> deleteById(@PathVariable Long id) {
        postService.delete(id);
        return ApiResponseUtil.success();
    }

    @SaCheckPermission(value = "system:post:update", orRole = "root")
    @Operation(summary = "更新岗位")
    @OperationLog(moduleName = "岗位管理", eventName = "更新岗位")
    @PostMapping("update")
    public ApiResponse<Object> update(@Valid @RequestBody SysPost sysPost) {
        postService.update(sysPost);
        return ApiResponseUtil.success();
    }

    @SaCheckPermission(value = "system:post:list", orRole = "root")
    @Operation(summary = "岗位分页列表")
    @GetMapping("listPage")
    public ApiResponse<Pagination<SysPost>> listPage(SysPostReq sysPostReq) {
        Page<SysPost> postEntityPage = postService.pageList(sysPostReq);
        return ApiResponseUtil.success(PaginationUtil.toPagination(postEntityPage));
    }

    @Operation(summary = "岗位分页列表")
    @GetMapping("list")
    public ApiResponse<List<SysPost>> list(SysPostReq sysPostReq) {
        List<SysPost> sysPosts = postService.list(sysPostReq);
        return ApiResponseUtil.success(sysPosts);
    }
}
